package com.zealfi.wk.common;

import net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator;
import net.sf.log4jdbc.sql.resultsetcollector.ResultSetCollector;
import net.sf.log4jdbc.sql.resultsetcollector.ResultSetCollectorPrinter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * Created by kuan at 2017-03-31 下午 5:52
 */
public class SimpleDelegator extends Slf4jSpyLogDelegator {
    private final int MAX_LENGTH = 200;
    private final int MAX_ROWS = 20;

    private final Logger resultSetTableLogger = LoggerFactory.getLogger("jdbc.resultsettable");

    @Override
    public void resultSetCollected(ResultSetCollector resultSetCollector) {
        String resultsToPrint = new MyResultSetCollectorPrinter().getResultSetToPrint(resultSetCollector);
        if (resultsToPrint != null) {
            resultSetTableLogger.info(resultsToPrint);
        }
    }

    class MyResultSetCollectorPrinter extends ResultSetCollectorPrinter {
        private StringBuffer table = new StringBuffer();

        @Override
        public String getResultSetToPrint(ResultSetCollector resultSetCollector) {
            List<List<Object>> lists;
            boolean flag = false;
            if (resultSetCollector.getRows() != null) {
                lists = resultSetCollector.getRows();
                if (lists.size() > MAX_ROWS) {
                    lists = lists.subList(0, MAX_ROWS);
                    flag = true;
                }
            } else {
                return null;
            }

            this.table.append(System.getProperty("line.separator"));

            int columnCount = resultSetCollector.getColumnCount();
            int maxLength[] = new int[columnCount];

            for (int column = 1; column <= columnCount; column++) {
                maxLength[column - 1] = resultSetCollector.getColumnName(column).length();
            }
            for (List<Object> printRow : lists) {
                int colIndex = 0;
                for (Object v : printRow) {
                    if (v != null) {
                        int length = v.toString().length();
                        if (length > MAX_LENGTH) {
                            continue;
                        }
                        if (length > maxLength[colIndex]) {
                            maxLength[colIndex] = length;
                        }
                    }
                    colIndex++;
                }
            }
            for (int column = 1; column <= columnCount; column++) {
                maxLength[column - 1] = maxLength[column - 1] + 1;
            }

            this.table.append("|");

            for (int column = 1; column <= columnCount; column++) {
                this.table.append(padRight("-", maxLength[column - 1]).replaceAll(" ", "-")
                        + "|");
            }
            this.table.append(System.getProperty("line.separator"));
            this.table.append("|");
            for (int column = 1; column <= columnCount; column++) {
                this.table.append(padRight(resultSetCollector.getColumnName(column),
                        maxLength[column - 1])
                        + "|");
            }
            this.table.append(System.getProperty("line.separator"));
            this.table.append("|");
            for (int column = 1; column <= columnCount; column++) {
                this.table.append(padRight("-", maxLength[column - 1]).replaceAll(" ", "-")
                        + "|");
            }
            this.table.append(System.getProperty("line.separator"));
            for (List<Object> printRow : lists) {
                int colIndex = 0;
                this.table.append("|");
                for (Object v : printRow) {
                    if (v != null && v.toString().length() > MAX_LENGTH) {
                        v = "内容过长,不显示";
                    }
                    this.table.append(padRight(v == null ? "null" : v.toString(),
                            maxLength[colIndex])
                            + "|");
                    colIndex++;
                }
                this.table.append(System.getProperty("line.separator"));
            }
            this.table.append("|");
            for (int column = 1; column <= columnCount; column++) {
                this.table.append(padRight("-", maxLength[column - 1]).replaceAll(" ", "-")
                        + "|");
            }

            this.table.append(System.getProperty("line.separator"));
            if (flag) {
                this.table.append("结果过多,只显示前" + MAX_ROWS + "行");
            }

            resultSetCollector.reset();

            return this.table.toString();

        }

        private String padRight(String s, int n) {
            return String.format("%1$-" + n + "s", s);
        }
    }
}
